package com.paddyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.page.MyPage;
import com.paddyi.commons.qiniu.MyQiniu;
import com.paddyi.commons.utils.MD5;
import com.paddyi.commons.utils.StringUtils;
import com.paddyi.model.SsoUser;
import com.paddyi.service.ISsoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  (SsoUser)
 * @author paddy
 * @since  2019-07-28 05:01:19
 */
@RestController
@RequestMapping("ssoUser")
public class SsoUserController extends BaseController{

	@Autowired
	private ISsoUserService ssoUserService;



    @PostMapping(value="/addImg")
    public HttpResult addImg(MultipartFile file) {
        String date = new Date().getTime()+"";
        String fileName = file.getOriginalFilename();
        String[] fileNames = fileName.split("\\.");
        String type = fileNames[1];
        String name = date+"."+type;
        MyQiniu.uploadMultipartFile(file,name,true);
        SsoUser ssoUser = ssoUserService.getById(getUserId());
        String img = "http://qiniu.paddyi.com/"+name+"?imageView2/1/w/200/h/200/q/75|imageslim";
        ssoUser.setImg(img);
        ssoUserService.updateById(ssoUser);
        return HttpResult.ok(img);
    }
    /**
     * 获取用户信息
     */
    @GetMapping(value = "getUser")
    public HttpResult getUser(){
        return HttpResult.ok(getCurrentUser());
    }
    /**
     * 删除
     * @param records
     * @return
     */
	@PostMapping(value="/del")
	public HttpResult del(@RequestBody List<Long> records) {
		return HttpResult.ok(ssoUserService.removeByIds(records));
	}

	/**
     * 修改
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody SsoUser record) {
        SsoUser ssoUser = ssoUserService.getById(getUserId());
        ssoUser.setBirthday(record.getBirthday());
        ssoUser.setEmail(record.getEmail());
        ssoUser.setName(record.getName());
        ssoUser.setPhone(record.getPhone());
        ssoUser.setSex(record.getSex());
        return HttpResult.ok(ssoUserService.updateById(ssoUser));
    }

    /**
     * 修改密码
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/editPassword")
    public HttpResult editPassword(@RequestBody SsoUser record) {
        SsoUser ssoUser = ssoUserService.getById(getUserId());
        String passWord =  MD5.getMd5Str(record.getPlaintext());
        if(passWord.equals(ssoUser.getPassword())){
            ssoUser.setPassword(MD5.getMd5Str(record.getPassword()));
            ssoUser.setPlaintext(record.getPassword());
            return HttpResult.ok(ssoUserService.updateById(ssoUser));
        }
        return HttpResult.error("旧密码填写错误！");
    }


    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     * @param page
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody MyPage<SsoUser> page) {
	    if(page.getQuerycondition()!=null){
            SsoUser ssoUser = page.getQuerycondition().get(0);
            QueryWrapper<SsoUser> queryWrapper = new QueryWrapper<>();
            for(Field f:ssoUser.getClass().getDeclaredFields()){
                f.setAccessible(true);
                try {
                    if(f.get(ssoUser) != null && !"serialVersionUID".equals(f.getName())){
                        queryWrapper.eq(f.getName(), f.get(ssoUser));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return HttpResult.ok(ssoUserService.page(page,queryWrapper));
        }
		return HttpResult.ok(ssoUserService.page(page));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/find")
	public HttpResult find(@RequestParam Long id) {
		return HttpResult.ok(ssoUserService.getById(id));
	}
}
